#! /usr/bin/env python
#coding=utf-8

from get_month_quarter import *
import time
db = get_db_connection()

#----------------------------------------#
def run_notice_to_message():
    from uliweb import settings, function
    from uliweb.utils.common import Serial
    #从settings.ini获取任务类型编号
    settings = Ini('../apps/settings.ini')
#    local_settings = Ini('../apps/local_settings.ini')
#    #发邮件初始化
#    mail_obj.redis = function('get_redis')()
    
#    today = datetime.date.today()
    #assignment_type = dict(settings.PARA.ASSIGNMENT_TYPE)
    req_notice = get_model('notice')
    req_message = get_model('message')
    notice_tables = dict(settings.Audit.notice_tables)
#    day_buffer = milestone_buffer.get(buffer)
    obj_notice = req_notice.filter()
    message_data=[]
    for row in obj_notice:
        #将notice中消息组织成提示信息写入message表

        Table = get_model(row.table_id.table_name)
#        print row.table_id.table_name
#        print Table
        str_link = notice_tables.get(row.table_id.table_name)
        if not str_link:
            str_link = ''
        if Table:
            obj = Table.get(row.obj_id)
#            print row.obj_id
            if not getattr(obj,'get_receiver_ids',None):
#               print 'xxxxxxxxxxxxxxxx'
               continue 
            if obj:
                users = obj.get_receiver_ids(row._sender_)
#                print 'users',users
                for row1 in users:
                    #get_init_message_data为message数据初始化函数
                    task_message = get_init_message_data()
                    if row.modify_flag == '1':
                        task_message['memo'] = str(Tag('a', "<font color='#238E68'>"+u'新增|'+row.memo+"</font>", href=str_link+str(row.obj_id),target='_blank'))
                    elif row.modify_flag == '2':
                        task_message['memo'] = str(Tag('a', "<font color='#ff8800'>"+u'修改|'+row.memo+"</font>", href=str_link+str(row.obj_id),target='_blank'))
                    elif row.modify_flag == '3':
                        task_message['memo'] = "<font color='red'>"+str(u'删除|'+str(row.obj_id)+row.memo)+"</font>"
                    
                    #task_message['memo'] = "<font color='red'>"+str(Tag('a', u'任务延迟', href=str('/tasks/develop/list'+'?'+'task_id'+'='+row.task_id),target='_blank'))+"</font>"
                    task_message['type'] = '2'                    
                    task_message['user'] = row1
                    task_message['read_flag'] = False
                    task_message['sender'] = row.sender
                    message_data.append(task_message)
#                    print 'task_message',task_message
#                #新增问题或风险给管理组发邮件
#                if row.modify_flag == '1' and (row.table_id.table_name == 'question' or row.table_id.table_name == 'risk'):
#                    txt = str(Tag('a', "<font color='#238E68'>"+u'新增|'+row.memo+"</font>", href=str_link+str(row.obj_id),target='_blank'))
#                    mail = {'from':settings.get_var('PARA/EMAIL_SENDER'), 'to':settings.get_var('PARA/ADM_TEAM'),
#                        'title':row.sender + u'新提出问题或风险！', 'message':txt}
#                    _t = Serial.dump(mail)
#                    mail_obj.redis.lpush('send_mails', _t)
                    
                
                #提交生产前需要将以下删除语句注释去掉
                req_notice.remove(and_(req_notice.c.id == row.id))
    #save_new_message函数为message数据保存函数
#    print message_data
    save_new_message(message_data)

while True:
            
    if __name__ == '__main__':
        try:
            db.begin()
            run_notice_to_message()
            db.commit()
            log.info("finish")
        except Exception, e:
            db.rollback()
            log.exception(e)
            log.error('error')
            raise
#    print '---------------------'
    time.sleep(600)
    

